package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hls.dao.GoodsDao;
import cn.hls.entity.Goods;
import cn.hls.entity.vo.PageInfo;
import cn.hls.exception.BussinessException;
import cn.hlsconstant.DataAssessEnum;

 
 
/**
  *@fileName: GoodsDaoImpl.java
  *@package: cn.hls.dao.impl
　*@Description:货物数据操作接口实现类
　*@author: uuhui@163.com
  *@date: 2016年6月9日
 *
 */
@Component("goodsDao")
public class GoodsDaoImpl extends BaseDaoImpl<Goods> implements GoodsDao {
	@Override
	public PageInfo<Goods> loadGoodsInfo(Goods entity, PageInfo<Goods> page) throws Exception {
		// 定义查询Hql语句
		StringBuilder selectHql=new StringBuilder("from Goods p inner join fetch p.category c left join fetch p.safetyCard sc  ");
		// 定义汇总Hql语句
		StringBuilder countHql = new StringBuilder("select count(p.id) from Goods p inner join  p.category c left join  p.safetyCard sc ");
		//定义查询条件hql语句
		StringBuilder condHql=new StringBuilder("where p.deleted=? ");
		// 定义查询参数
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(false);
		if (entity != null) {
			//按名称查询
			 if(StringUtils.hasText(entity.getName())){
				 condHql.append("and p.name like ? ");
				 paramList.add("%"+entity.getName().trim()+"%");
			 }
			 //按编码查询
			 if(StringUtils.hasText(entity.getCode())){
				 condHql.append("and p.code like ? ");
				 paramList.add("%"+entity.getCode().trim()+"%");
			 }
			 //按货物类型信息
			 if(entity.getCategory()!=null && entity.getCategory().getId()!=null){
				 condHql.append("and c.id=? ");
				 paramList.add(entity.getCategory().getId());
			 }
		}
		countHql. append(condHql);
		selectHql.append(condHql);
		selectHql.append(" order by p.createTime");
		return find(selectHql.toString(), countHql.toString(), paramList.toArray(), page);
	}
	
	@Override
	public List<Goods> loadGoodsList(Goods entity)throws Exception{
		// 定义查询Hql语句
		StringBuilder selectHql=new StringBuilder("from Goods p inner join fetch p.category c left join fetch p.safetyCard sc  where p.deleted=? ");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(false);
		if (entity != null) {
			//按名称查询
			 if(StringUtils.hasText(entity.getName())){
				 selectHql.append("and name like ? ");
				 paramList.add("%"+entity.getName().trim()+"%");
			 }
			 //按编码查询
			 if(StringUtils.hasText(entity.getCode())){
				 selectHql.append("and code like ? ");
				 paramList.add("%"+entity.getCode().trim()+"%");
			 }
			 
		}
		return findByHql(selectHql.toString(), paramList.toArray());
	}

	@Override
	public void delete(Object entityId) throws Exception {
		String []ids=(String[])entityId;
		StringBuilder hql=new StringBuilder("update Goods set deleted=? where id in (");
		List<Object>params=new ArrayList<Object>();
		params.add(true);
		for (String id:ids){
			hql.append("?,");
			params.add(Long.valueOf(id));
		}	
		hql.delete(hql.length()-1, hql.length());
		hql.append(")");
		 try {
			executeHQLUpdate(hql.toString(), params.toArray());
		} catch (Exception e) {
			e.printStackTrace();
			throw new BussinessException(DataAssessEnum.UPDATE_ERROR,"删除货物信息失败" );
		}
	}

	
	@Override
		public boolean exists(Goods entity)throws Exception{
			StringBuilder selectHql=new StringBuilder("select count(id) from Goods p where deleted=? ");
			List<Object>params=new ArrayList<Object>();
			params.add(false);
			if(entity!=null){
				if(StringUtils.hasText(entity.getCode())){
					selectHql.append("and p.code=? ");
					params.add(entity.getCode().trim());
				}
				 
			}
			Query query=this.getSession().createQuery(selectHql.toString());
			for(int i=0;i<params.size();i++){
				query.setParameter(i, params.get(i));
			}
			Long c = Long.valueOf(((Number) query.uniqueResult()).longValue());
			return c.intValue()>0?true:false;
	}
	
}
